from CvPythonExtensions import *

from PlanetfallSpells import PlanetfallSpellUnitTargetBase
import PlanetfallSpellsUtils as PSUtils

gc = CyGlobalContext()

SACRIFICE_CAST_RANGE = 2

class Sacrifice(PlanetfallSpellUnitTargetBase):
    def __init__(self):
        PlanetfallSpellUnitTargetBase.__init__(self)

        self.pCastingUnits = []
        # Fill list with units that can cast, such as..
        #self.pCastingUnits.append( gc.getInfoTypeForString("UNIT_NAME_1") )
        #self.pCastingUnits.append( gc.getInfoTypeForString("UNIT_NAME_2") )

    def canUnitCast(self, pCastingUnit, pCastingPlot, bVisible):
        if not pCastingUnit.getUnitType() in self.pCastingUnits:
            return False
        
        return True

    def getRangePlots(self, pCastingGroup, pCastingPlot, pHoverPlot):
        pRangePlots = PSUtils.getRangePlotDistanceList(pCastingPlot, SACRIFICE_CAST_RANGE)
        return pRangePlots
        

    def canTargetPlot(self, pCastingGroup, pCastingPlot, pPotentialPlot):
        if not PSUtils.isPlotDistanceInRange(pCastingPlot, SACRIFICE_CAST_RANGE, pPotentialPlot):
            return False

        if not pPotentialPlot.isVisible(pCastingGroup.getTeam(), False):
            return False

        return True


    def startSpellResultsOnUnit(self, pCastingGroup, pTargetUnit):
        # Find the first unit who can cast this spell and kill them
        for pUnit in pCastingGroup.getUnits():
            if self.canUnitCast(pUnit, pUnit.plot(), False):
                CyEngine().triggerEffect( 40, pUnit.plot().getPoint() )
                pUnit.kill(True, pUnit.getOwner())

        # Kill the enemy
        CyEngine().triggerEffect( 40, pTargetUnit.plot().getPoint() )
        pTargetUnit.kill(True, pUnit.getOwner())
